﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.IdentityModel.Claims;
using System.ServiceModel;
using Microsoft.IdentityModel.Protocols.WSTrust;
using Veracruz.IdentityModel;
using System.IdentityModel.Tokens;
using System.Web.UI;
using AjaxControlToolkit;

namespace Veracruz.Facebook.Sharepoint.Webparts
{
    public class CreatePeople : WebPart
    {
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.Controls.Clear();
            if (ClaimsPrincipal.Current.Identity.IsAuthenticated == false)
            {
                Label lbl = new Label();
                lbl.Text = "Not authenticated";
                this.Controls.Add(lbl);
                return;
            }

            SecurityToken __token = Page.Session["cachedToken"] as SecurityToken;

            SPContext context = SPContext.Current;
            SPUserCollection users = context.Web.AllUsers;
            string strCurrentUser = "";
            SPUser curUser = null;
            foreach (SPUser usr in users)
                if (usr.Name == ClaimsPrincipal.Current.Identity.Name)
                {
                    curUser = usr;
                    break;
                }

            if (ClaimsPrincipal.Current.Identities.Count > 0 && ClaimsPrincipal.Current.Identity.IsAuthenticated)
            {
                IClaimsIdentity claimsIdentity = ClaimsPrincipal.Current.Identities[0];
                strCurrentUser = claimsIdentity.Name;
            }
            else
            {
                Label lblNoClaims = new Label();
                lblNoClaims.CssClass = "ms-descriptiontext";
                this.Controls.Add(lblNoClaims);
                return;
            }
            if (ViewState["curView"] != null && ViewState["curView"].ToString() == "ShowWizard")
            {
                Panel pnl = this.FindControl("pnlFriends") as Panel;
                if (pnl != null)
                    pnl.Visible = false;
                Wizard wz = null;
                CreateWizard(ref wz);
                UpdatePanel upd = null;
                CreateUpdatePanel(ref upd);
                upd.ContentTemplateContainer.Controls.Add(wz);
                this.Controls.Add(upd);
                return;
            }
            if (curUser.IsSiteAdmin || ViewState["curView"].ToString() == "ShowFriends")
            {
                ChannelFactory<ISocialNetworkService> l_channelFactory = new ChannelFactory<ISocialNetworkService>("FacebookService");
                WSTrustClientCredentials clientCreds;
                ISocialNetworkService l_service = WSTrustClientCredentials.ConfigureChannelFactory<ISocialNetworkService>(l_channelFactory, out clientCreds);
                clientCreds.ActAs = __token;

                UserInfo[] l_friends = l_service.GetFriendFullDetails("uid");
                showTableFriends(l_friends);
                return;
            }
            else
            {
                UpdatePanel upd = null;
                CreateUpdatePanel(ref upd);
                Wizard wz = null;
                CreateWizard(ref wz);
                upd.ContentTemplateContainer.Controls.Add(wz);
                this.Controls.Add(upd);

            }
        }
        void showTableFriends(UserInfo[] arUI)
        {
            ViewState["curView"] = "ShowFriends";
            UpdatePanel updPanel = null;
            CreateUpdatePanel(ref updPanel);

            Panel pnlFriends = new Panel();
            pnlFriends.ID = "pnlFriends";
            pnlFriends.Visible = true;
            Table tblFriends = new Table();
            pnlFriends.Controls.Add(tblFriends);
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.CssClass = "ms-gb";
            tc.ColumnSpan = 2;
            tc.Text = "Friends sign in on veracruz";
            tr.Cells.Add(tc);
            tblFriends.Rows.Add(tr);
            foreach (UserInfo ui in arUI)
            {
                if (ui.Email != null && ui.Email != string.Empty)
                {
                    tr = new TableRow();
                    tc = new TableCell();
                    tc.CssClass = "ms-gb";
                    Image img = new Image();
                    if (ui.SmallPictureUrl == string.Empty)
                        img.ImageUrl = "http://veracruz/images/noimage.jpg";
                    else
                        img.ImageUrl = ui.SmallPictureUrl;
                    img.Width = new Unit(50);
                    tc.Controls.Add(img);
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    tc.CssClass = "ms-vb2";
                    LinkButton lb = new LinkButton();
                    lb.Attributes.Add("emailUser",ui.Email);
                    lb.Click += new EventHandler(lb_Click);
                    lb.Text = ui.FirstName + " " + ui.LastName;
                    tc.Controls.Add(lb);
                    tr.Cells.Add(tc);
                    tblFriends.Rows.Add(tr);
                }


            }
            updPanel.ContentTemplateContainer.Controls.Add(pnlFriends);
            this.Controls.Add(updPanel);
        }
        void ShowUserProfil(UserInfo ui)
        {

            Table tbl = new Table();
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.CssClass = "ms-gb";
            tc.Text = "Civil State";
            tc.ColumnSpan = 2;
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.CssClass = "ms-gb";
            tc.Text = "Competence";
            tc.ColumnSpan = 2;
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.CssClass = "ms-gb";
            tc.Text = "Morality";
            tc.ColumnSpan = 2;
            tr.Cells.Add(tc);

            tr = new TableRow();
            tc = new TableCell();
            tc.CssClass = "ms-vb2";
            tc.Text = "FirstName";
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.CssClass = "ms-vb2";
            tc.Text = ui.FirstName;
            tr.Cells.Add(tc);

        }
        void lb_Click(object sender, EventArgs e)
        {

            UpdatePanel upd = null;
            CreateUpdatePanel(ref upd);
            upd.ContentTemplateContainer.Controls.Clear();
            Wizard wz = null;
            CreateWizard(ref wz);
            LinkButton lb = sender as LinkButton;
            ViewState["UserSelectName"] = lb.Text;
            ViewState["UserSelectEmail"] = lb.Attributes["emailUser"];

            upd.ContentTemplateContainer.Controls.Add(wz);
        }
        private void CreateUpdatePanel(ref UpdatePanel upd)
        {
            this.EnsureUpdatePanelFixups();
            if (ScriptManager.GetCurrent(this.Page) == null)
            {
                this.Controls.Add(new ScriptManager() { EnablePartialRendering = true });
            }

            upd = this.FindControl("UpdatePanel1") as UpdatePanel;
            if (upd == null)
            {
                upd = new UpdatePanel();
                upd.ID = "UpdatePanel1";
                upd.ChildrenAsTriggers = true;

                upd.UpdateMode = UpdatePanelUpdateMode.Conditional;
            }

        }
        private void CreateWizard(ref Wizard wz)
        {
            ViewState["curView"] = "ShowWizard";
            wz = new Wizard();
            wz.SideBarStyle.VerticalAlign = VerticalAlign.Top;
            wz.StepStyle.VerticalAlign = VerticalAlign.Top;
            wz.StepStyle.HorizontalAlign = HorizontalAlign.Left;
            wz.Width = new Unit(100, UnitType.Percentage);
            wz.StepStyle.Width = new Unit(100, UnitType.Percentage);
            wz.ID = "wizardCreatePeople";
            wz.FinishButtonClick += new WizardNavigationEventHandler(wz_FinishButtonClick);


            WizardStep ws = new WizardStep();
            wz.WizardSteps.Add(ws);
            ws.StepType = WizardStepType.Start;
            ws.ID = "StepStart";
            ws.Title = "Select Archetype";
            Panel pnl = null;
            CreateSelectArch(ref pnl);
            ws.Controls.Add(pnl);

            ws = new WizardStep();
            wz.WizardSteps.Add(ws);
            ws.StepType = WizardStepType.Step;
            ws.ID = "Step1";
            ws.Title = "Salary or time";
            pnl = null;
            CreateSalairyOrTime(ref pnl);
            ws.Controls.Add(pnl);

            ws = new WizardStep();
            wz.WizardSteps.Add(ws);
            ws.StepType = WizardStepType.Step;
            ws.ID = "Step2";
            ws.Title = "Choose you social trait";
            pnl = null;
            CreateTrait(ref pnl);
            ws.Controls.Add(pnl);

            ws = new WizardStep();
            wz.WizardSteps.Add(ws);
            ws.StepType = WizardStepType.Step;
            ws.ID = "Step3";
            ws.Title = "Choose you Social Skill";
            pnl = null;
            CreateSocialSkill(ref pnl);
            ws.Controls.Add(pnl);

            ws = new WizardStep();
            wz.WizardSteps.Add(ws);
            ws.StepType = WizardStepType.Step;
            ws.ID = "Step4";
            ws.Title = "Choose you Professionnal Competence";
            pnl = null;
            CreateProfessionalCompetence(ref pnl);
            ws.Controls.Add(pnl);

            ws = new WizardStep();
            wz.WizardSteps.Add(ws);
            ws.StepType = WizardStepType.Finish;
            ws.ID = "StepFinish";
            ws.Title = "Finish";
            pnl = null;
            CreateCompletion(ref pnl);
            ws.Controls.Add(pnl);

        }

        void wz_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            Table tblArch = this.FindControl("wizardCreatePeople").FindControl("StepStart").FindControl("pnlSelectArch").FindControl("tblSelectArch").FindControl("tblDetail") as Table;
            Label lblArch = tblArch.FindControl("lblArchName") as Label;
            TextBox txtMoney = this.FindControl("wizardCreatePeople").FindControl("Step1").FindControl("pnlSalairy").FindControl("tblSalairy").FindControl("txtSalairyOrTime") as TextBox;
            ListBox lbTrait= this.FindControl("wizardCreatePeople").FindControl("Step2").FindControl("pnlTraitSelect").FindControl("tblTraitSelect").FindControl("lbTrait") as ListBox;
            TextBox txtSkillAttractive = this.FindControl("wizardCreatePeople").FindControl("Step3").FindControl("pnlSocialSkill").FindControl("tblSocialSkill").FindControl("tbAttractive") as TextBox;
            TextBox txtSkillEmpathy = this.FindControl("wizardCreatePeople").FindControl("Step3").FindControl("pnlSocialSkill").FindControl("tblSocialSkill").FindControl("tbEmpathy") as TextBox;
            TextBox txtSkillFastTalk = this.FindControl("wizardCreatePeople").FindControl("Step3").FindControl("pnlSocialSkill").FindControl("tblSocialSkill").FindControl("tbFastTalk") as TextBox;
            TextBox txtSkillIntimidating = this.FindControl("wizardCreatePeople").FindControl("Step3").FindControl("pnlSocialSkill").FindControl("tblSocialSkill").FindControl("tbIntimidating") as TextBox;
            TextBox txtSkillSubterfuge = this.FindControl("wizardCreatePeople").FindControl("Step3").FindControl("pnlSocialSkill").FindControl("tblSocialSkill").FindControl("tbSubterfuge") as TextBox;
            TextBox txtSkillSeductive = this.FindControl("wizardCreatePeople").FindControl("Step3").FindControl("pnlSocialSkill").FindControl("tblSocialSkill").FindControl("tbSeductive") as TextBox;
            SPList lstCompetence = SPContext.Current.Web.Lists["Competence"];
            Dictionary<string,TextBox> arCompetence = new Dictionary<string,TextBox>();
            foreach (SPListItem item in lstCompetence.Items)
            {
                TextBox txtCompetence = this.FindControl("wizardCreatePeople").FindControl("Step4").FindControl("pnlCompetencePro").FindControl("tblCompetencePro").FindControl("txt" + item["Title"].ToString()) as TextBox;
                arCompetence.Add(item["Title"].ToString(), txtCompetence); 
            }
            SPList lstGamer = SPContext.Current.Web.Lists["Gamer"];
            SPListItem newItem= lstGamer.Items.Add();
            SPList lstArch = SPContext.Current.Web.Lists["Archetype"];
            SPQuery query = new SPQuery();
            query.Query= "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + lblArch.Text + "</Value></Eq></Where>";
            SPListItem itemArch= lstArch.GetItems(query)[0];
            if (ViewState["UserSelectName"] != null)
            {
                newItem["Email"] = ViewState["UserSelectName"].ToString();
                newItem["UserName"] = ViewState["UserSelectEmail"].ToString();
            }
            else
            {
                newItem["Email"] = ClaimsPrincipal.Current.Identities[0].Claims[9].Value;
                newItem["UserName"] = ClaimsPrincipal.Current.Identities[0].Claims[0].Value;
            }
            newItem["Archetype"] = int.Parse(itemArch["ID"].ToString());
            newItem["Wage"] = txtMoney.Text;
            newItem["Traits"] = int.Parse(lbTrait.SelectedItem.Value);
            newItem["SocialSkills"] = txtSkillAttractive.Text + ";" + txtSkillEmpathy.Text + ";" + txtSkillFastTalk.Text + ";" + txtSkillIntimidating.Text + ";" + txtSkillSubterfuge.Text + ";" + txtSkillSeductive.Text;
            string strCompetence = "";
            foreach (string strComp in arCompetence.Keys)
            {
                strCompetence += strComp;
                strCompetence += ",";
                strCompetence += arCompetence[strComp].Text;
                strCompetence += ";";
            }
            newItem["Competences"] = strCompetence.Substring(0, strCompetence.Length - 1);
            newItem.Update();
            lstGamer.Update();
        }
        private void CreateProfessionalCompetence(ref Panel pnl)
        {
            pnl = new Panel();
            pnl.ID = "pnlCompetencePro";
            if (ViewState["vsArchName"] == null)
                return;
            Table tbl = new Table();
            tbl.ID = "tblCompetencePro";
            pnl.Controls.Add(tbl);
            TableRow tr = null;
            TableCell tc = null;
            SPList lstCompetence = SPContext.Current.Web.Lists["Competence"];
            SPList lstCompetenceLink = SPContext.Current.Web.Lists["CompetenceArchetypeLink"];
            string strArchName = ViewState["vsArchName"].ToString();
            SPQuery query=new SPQuery();
            query.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + strArchName + "</Value></Eq></Where>";
            SPItem lstArchetype = SPContext.Current.Web.Lists["Archetype"].GetItems(query)[0];
            foreach (SPListItem item in lstCompetence.Items)
            {
                tr = new TableRow();
                tc = new TableCell();
                tc.Text = item["Title"].ToString();
                tr.Cells.Add(tc);
                tc = new TableCell();
                TextBox txt = new TextBox();
                txt.ID = "txt" + item["Title"].ToString();
                SliderExtender slider = new SliderExtender();
                slider.TargetControlID = "txt" + item["Title"].ToString(); 
                slider.Length = 200;
                slider.Decimals = 1;
                slider.Minimum = 0;
                slider.Maximum = 5;
                SPFieldLookupValueCollection col = lstArchetype["Competence"] as SPFieldLookupValueCollection;
                if (lstArchetype["Competence"].ToString().Contains(item["Title"].ToString()))
                {
                    SPFieldLookupValue val = col.Find(x => x.LookupValue.Contains(item["Title"].ToString()));
                    txt.Text=lstCompetenceLink.GetItemById(val.LookupId)["Quantity"].ToString();
                }
                tc.Controls.Add(txt);
                tc.Controls.Add(slider);
                tr.Cells.Add(tc);
                tbl.Rows.Add(tr);
                
            }
        }
        private void CreateSelectArch(ref Panel pnl)
        {
            pnl = new Panel();
            pnl.ID = "pnlSelectArch";
            Table tbl = new Table();
            pnl.Controls.Add(tbl);
            tbl.ID = "tblSelectArch";
            TableRow tr = new TableRow();
            tbl.Rows.Add(tr);
            tr.ID = "trSelectArch1";
            TableCell tc = new TableCell();
            tc.ID = "tcSelectArchTreeview";
            TreeView tvw = new TreeView();
            CreateTreeview(ref tvw);
            tc.Controls.Add(tvw);
            tc.VerticalAlign = VerticalAlign.Top;
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tr.Cells.Add(tc);


            Table tblDetail = new Table();
            tblDetail.ID = "tblDetail";
            tc.ID = "tcSelectArchDetail";
            tc.VerticalAlign = VerticalAlign.Top;
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Controls.Add(tblDetail);

            TableRow trDetail = new TableRow();
            TableCell tcDetail = new TableCell();
            tcDetail.CssClass = "ms-gb";
            tcDetail.ID = "tcDetailArchName";
            trDetail.ID = "trDetail1";
            tcDetail.Text = "Archetype";
            trDetail.Cells.Add(tcDetail);

            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchNameContent";
            tcDetail.CssClass = "ms-vb2";
            Label lbl = new Label();
            lbl.CssClass = "ms-vb2";
            lbl.ID = "lblArchName";
            tcDetail.Controls.Add(lbl);
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchDesc";
            trDetail.ID = "trDetail2";
            tcDetail.CssClass = "ms-gb";
            tcDetail.Text = "Description";
            trDetail.Cells.Add(tcDetail);

            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchDescContent";
            tcDetail.CssClass = "ms-vb2";
            lbl = new Label();
            lbl.CssClass = "ms-vb2";
            lbl.ID = "lblArchDesc";
            tcDetail.Controls.Add(lbl);
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchWages";
            trDetail.ID = "trDetail3";
            tcDetail.CssClass = "ms-gb";
            tcDetail.Text = "Average wages";
            trDetail.Cells.Add(tcDetail);

            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchWagesContent";
            tcDetail.CssClass = "ms-vb2";
            lbl = new Label();
            lbl.CssClass = "ms-vb2";
            lbl.ID = "lblArchWages";
            tcDetail.Controls.Add(lbl);
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchTAmount";
            trDetail.ID = "trDetail4";
            tcDetail.CssClass = "ms-gb";
            tcDetail.Text = "Time Amount";
            trDetail.Cells.Add(tcDetail);

            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchTAmountContent";
            tcDetail.CssClass = "ms-vb2";
            lbl = new Label();
            lbl.CssClass = "ms-vb2";
            lbl.ID = "lblArchTAmount";
            tcDetail.Controls.Add(lbl);
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchNetwork";
            trDetail.ID = "trDetail5";
            tcDetail.CssClass = "ms-gb";
            tcDetail.Text = "Network";
            trDetail.Cells.Add(tcDetail);

            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchNetworkContent";
            tcDetail.CssClass = "ms-vb2";
            lbl = new Label();
            lbl.CssClass = "ms-vb2";
            lbl.ID = "lblArchNetwork";
            tcDetail.Controls.Add(lbl);
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchServices";
            trDetail.ID = "trDetail6";
            tcDetail.CssClass = "ms-gb";
            tcDetail.Text = "Services";
            trDetail.Cells.Add(tcDetail);

            tcDetail = new TableCell();
            tcDetail.ID = "tcDetailArchServicesContent";
            tcDetail.CssClass = "ms-vb2";
            lbl = new Label();
            lbl.CssClass = "ms-vb2";
            lbl.ID = "lblArchServices";
            tcDetail.Controls.Add(lbl);
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

        }
        private void CreateTreeview(ref TreeView tvw)
        {
            SPContext context = SPContext.Current;
            SPList lstArch = context.Web.Lists["Archetype"];
            SPList lstSubDomain = context.Web.Lists["SubDomain"];
            SPList lstDomain = context.Web.Lists["Domain"];
            tvw = new TreeView();
            tvw.ID = "TreeViewArch";
            tvw.SelectedNodeChanged += new EventHandler(tvw_SelectedNodeChanged);

            foreach (SPListItem domain in lstDomain.Items)
            {
                TreeNode nodeDomain = new TreeNode();
                nodeDomain.Text = domain["Title"].ToString();
                foreach (SPListItem subDomain in lstSubDomain.Items)
                {
                    if (subDomain["Domain"].ToString().Contains(domain["Title"].ToString()))
                    {
                        TreeNode nodeSubDomain = new TreeNode();
                        nodeSubDomain.Text = subDomain["Title"].ToString();
                        foreach (SPListItem archetype in lstArch.Items)
                        {
                            if (archetype["SubDomain"].ToString().Contains(subDomain["Title"].ToString()))
                            {
                                TreeNode nodeArch = new TreeNode();
                                nodeArch.Text = archetype["Title"].ToString();
                                nodeArch.Value = archetype["ID"].ToString();
                                nodeArch.NavigateUrl = "";
                                nodeArch.SelectAction = TreeNodeSelectAction.Select;
                                nodeSubDomain.ChildNodes.Add(nodeArch);
                            }
                        }
                        nodeDomain.ChildNodes.Add(nodeSubDomain);
                    }
                }
                tvw.Nodes.Add(nodeDomain);
            }
        }
        private void CreateSalairyOrTime(ref Panel pnl)
        {
            pnl = new Panel();
            pnl.ID = "pnlSalairy";
            Table tbl = new Table();
            tbl.ID="tblSalairy";
            pnl.Controls.Add(tbl);
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.ColumnSpan = 3;
            tc.Text = "Preference Time or Salairy";
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);
            tr = new TableRow();
            tr.ID = "trSalairyOrTime";
            tc = new TableCell();
            tc.Text = "Salairy";
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.ID = "tcSalairyOrTime";
            SliderExtender slider = new SliderExtender();
            TextBox txt = new TextBox();
            txt.ID = "txtSalairyOrTime";
            txt.Text = "0";
            slider.TargetControlID = "txtSalairyOrTime";
            slider.Length = 200;
            slider.Decimals = 1;
            slider.Minimum = -2;
            slider.Maximum = 2;
            slider.Steps = 5;
            tc.Controls.Add(slider);
            tc.Controls.Add(txt);
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.Text = "Time";
            tr.Cells.Add(tc);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);
            pnl.Controls.Add(tbl);
        }
        private void CreateTrait(ref Panel pnl)
        {
            pnl = new Panel();
            pnl.ID = "pnlTraitSelect";
            Table tbl = new Table();
            tbl.ID = "tblTraitSelect";
            pnl.Controls.Add(tbl);
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            ListBox lb = null;
            CreateListTrait(ref lb);
            tc.Controls.Add(lb);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);
            tc = new TableCell();
            tbl.Rows.Add(tr);
            tr.Cells.Add(tc);
            Table tblDetail = new Table();
            tblDetail.ID = "tblTraitDetailSelect";
            tc.Controls.Add(tblDetail);
            TableRow trDetail = new TableRow();
            TableCell tcDetail = new TableCell();
            Label lbl = new Label();
            lbl.ID = "lblTraitName";
            tcDetail.Controls.Add(lbl);
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            tcDetail.Text = "Each trait can improve one of the 3 characteristics";
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            tcDetail.Text = "Delay";
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            lbl = new Label();
            lbl.ID = "lblTraitDelay";
            tcDetail.Controls.Add(lbl);
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            tcDetail.Text = "Security";
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            lbl = new Label();
            lbl.ID = "lblTraitSecurity";
            tcDetail.Controls.Add(lbl);
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            tcDetail.Text = "Friendlyness";
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);

            trDetail = new TableRow();
            tcDetail = new TableCell();
            lbl = new Label();
            lbl.ID = "lblTraitFriendlyness";
            tcDetail.Controls.Add(lbl);
            trDetail.Cells.Add(tcDetail);
            tblDetail.Rows.Add(trDetail);


        }
        private void CreateListTrait(ref ListBox lb)
        {
            lb = new ListBox();
            lb.ID = "lbTrait";
            lb.Rows = 9;
            SPList lstTrait = SPContext.Current.Web.Lists["SocialTrait"];
            foreach (SPListItem item in lstTrait.Items)
            {
                ListItem li = new ListItem(item["Title"].ToString(), item["ID"].ToString());
                lb.Items.Add(li);
            }
            lb.AutoPostBack = true;
            lb.SelectedIndexChanged += new EventHandler(lb_SelectedIndexChanged);
        }
        void lb_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox lb = sender as ListBox;
            if (lb == null)
                return;
            SPList lstTrait = SPContext.Current.Web.Lists["SocialTrait"];
            SPItem item = lstTrait.Items.GetItemById(int.Parse(lb.SelectedItem.Value));
            Table tblDetail = this.FindControl("wizardCreatePeople").FindControl("Step2").FindControl("pnlTraitSelect").FindControl("tblTraitSelect").FindControl("tblTraitDetailSelect") as Table;
            Label lblTraitDelay = tblDetail.FindControl("lblTraitDelay") as Label;
            Label lblTraitSecurity = tblDetail.FindControl("lblTraitSecurity") as Label;
            Label lblTraitFriendlyness = tblDetail.FindControl("lblTraitFriendlyness") as Label;
            lblTraitDelay.Text = item["Delay"].ToString();
            lblTraitSecurity.Text = item["Security"].ToString();
            lblTraitFriendlyness.Text = item["friendlyness"].ToString();
        }
        private void CreateSocialSkill(ref Panel pnl)
        {
            pnl = new Panel();
            pnl.ID = "pnlSocialSkill";
            Table tbl = new Table();
            tbl.ID = "tblSocialSkill";
            pnl.Controls.Add(tbl);
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.Text = "Attractive";
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.ID = "tcAttractive";
            TextBox tb = new TextBox();
            tb.ID = "tbAttractive";
            SliderExtender slider = new SliderExtender();
            slider.TargetControlID = "tbAttractive";
            slider.Length = 200;
            slider.Decimals = 1;
            slider.Minimum = 0;
            slider.Maximum = 5;
            tc.Controls.Add(tb);
            tc.Controls.Add(slider);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);


            tr = new TableRow();
            tc = new TableCell();
            tc.Text = "Empathy";
            tr.Cells.Add(tc);


            tc = new TableCell();
            tc.ID = "tcEmpathy";
            tb = new TextBox();
            tb.ID = "tbEmpathy";
            slider = new SliderExtender();
            slider.TargetControlID = "tbEmpathy";
            slider.Length = 200;
            slider.Decimals = 1;
            slider.Minimum = 0;
            slider.Maximum = 5;
            tc.Controls.Add(tb);
            tc.Controls.Add(slider);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);


            tr = new TableRow();
            tc = new TableCell();
            tc.Text = "Fast talk";
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.ID = "tcFastTalk";
            tb = new TextBox();
            tb.ID = "tbFastTalk";
            slider = new SliderExtender();
            slider.TargetControlID = "tbFastTalk";
            slider.Length = 200;
            slider.Decimals = 1;
            slider.Minimum = 0;
            slider.Maximum = 5;
            tc.Controls.Add(tb);
            tc.Controls.Add(slider);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);


            tr = new TableRow();
            tc = new TableCell();
            tc.Text = "Intimidating";
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.ID = "tcIntimidating";
            tb = new TextBox();
            tb.ID = "tbIntimidating";
            slider = new SliderExtender();
            slider.TargetControlID = "tbIntimidating";
            slider.Length = 200;
            slider.Decimals = 1;
            slider.Minimum = 0;
            slider.Maximum = 5;
            tc.Controls.Add(tb);
            tc.Controls.Add(slider);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);


            tr = new TableRow();
            tc = new TableCell();
            tc.Text = "Subterfuge";
            tr.Cells.Add(tc);


            tc = new TableCell();
            tc.ID = "tcSubterfuge";
            tb = new TextBox();
            tb.ID = "tbSubterfuge";
            slider = new SliderExtender();
            slider.TargetControlID = "tbSubterfuge";
            slider.Length = 200;
            slider.Decimals = 1;
            slider.Minimum = 0;
            slider.Maximum = 5;
            tc.Controls.Add(tb);
            tc.Controls.Add(slider);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);


            tr = new TableRow();
            tc = new TableCell();
            tc.Text = "Seductive";
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.ID = "tcSeductive";
            tb = new TextBox();
            tb.ID = "tbSeductive";
            slider = new SliderExtender();
            slider.TargetControlID = "tbSeductive";
            slider.Length = 200;
            slider.Decimals = 1;
            slider.Minimum = 0;
            slider.Maximum = 5;
            tc.Controls.Add(tb);
            tc.Controls.Add(slider);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);



        }
        void tvw_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeView tvw = sender as TreeView;
            if (tvw == null)
                return;
            SPList lstArch = SPContext.Current.Web.Lists["Archetype"];
            SPItem item = lstArch.Items.GetItemById(int.Parse(tvw.SelectedNode.Value));
            Table tblDetail = this.FindControl("wizardCreatePeople").FindControl("StepStart").FindControl("pnlSelectArch").FindControl("tblSelectArch").FindControl("tblDetail") as Table;
            Label lbl = tblDetail.FindControl("lblArchName") as Label;
            lbl.Text = tvw.SelectedNode.Text;
            ViewState["vsArchName"] = tvw.SelectedNode.Text;
            lbl = tblDetail.FindControl("lblArchDesc") as Label;
            lbl.Text = item["Description"] == null ? "" : item["Description"].ToString();
            lbl = tblDetail.FindControl("lblArchWages") as Label;
            lbl.Text = item["Average wages"] == null ? "" : item["Average wages"].ToString();
            lbl = tblDetail.FindControl("lblArchTAmount") as Label;
            lbl.Text = item["Time Amount"] == null ? "" : item["Time Amount"].ToString();
            lbl = tblDetail.FindControl("lblArchServices") as Label;
            lbl.Text = item["Services"] == null ? "" : item["Services"].ToString();
            lbl = tblDetail.FindControl("lblArchNetwork") as Label;
            string[] strNetwork = null;
            strNetwork = item["Network"] == null ? null : item["Network"].ToString().Split(';');
            lbl.Text = "";
            if (strNetwork != null)
            {
                for (int miind = 0; miind < strNetwork.Length; miind++)
                {
                    if (miind % 2 == 1)
                        lbl.Text += strNetwork[miind] + "<br>";

                }
                lbl.Text = lbl.Text.Replace("#", "");
            }
            else
            {
                lbl.Text = "";
            }
        }
        private void EnsureUpdatePanelFixups()
        {
            if (this.Page.Form != null)
            {
                string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
                if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
                {
                    this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
                }
            }
            ScriptManager.RegisterStartupScript(this, typeof(CreatePeople), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true);
        }

        private void CreateCompletion(ref Panel pnl)
        {
            pnl = new Panel();
            

        }
    }
}
